package com.atguigu.gulimail.coupon.controller;

import java.util.Arrays;
import java.util.Collections;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimail.coupon.entity.CouponEntity;
import com.atguigu.gulimail.coupon.service.CouponService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;



/**
 * 优惠券信息
 *
 * @author guoguo
 * @email 2418251830@qq.com
 * @date 2022-08-24 15:59:48
 */
@RestController
@RequestMapping("coupon/coupon")
@RefreshScope
public class CouponController {
    @Autowired
    private CouponService couponService;

    @Value("${coupon.user.name}")
    private String nameFromProperties;

    @Value("#{T(java.lang.Integer).parseInt('${coupon.user.age:5}')}")
    private Integer ageFromProperties;

    @Value("#{T(java.lang.Double).parseDouble('${coupon.user.money:999999999999.99}')}")
    private Double moneyFromYml;

    @Value("${coupon.user.work}")
    private String workFromProperties;

    /**
     * 测试，从配置文件中获取值
     * @return
     */
    @GetMapping("/testFromProperties")
    public R testFromProperties(){
        return R.ok().put("name",nameFromProperties).put("age",ageFromProperties)
                .put("money",moneyFromYml).put("work",workFromProperties);
    }

    /**
     * 模拟，查询当前会员的所有优惠券
     * @return
     */
    @GetMapping("/member/list")
    private R memberCouponsTest(){
        CouponEntity couponEntity = new CouponEntity();
        couponEntity.setCouponName("优惠券，满100减10");
        return R.ok().put("coupons7000", Collections.singletonList(couponEntity));
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("coupon:coupon:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = couponService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("coupon:coupon:info")
    public R info(@PathVariable("id") Long id){
		CouponEntity coupon = couponService.getById(id);

        return R.ok().put("coupon", coupon);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("coupon:coupon:save")
    public R save(@RequestBody CouponEntity coupon){
		couponService.save(coupon);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("coupon:coupon:update")
    public R update(@RequestBody CouponEntity coupon){
		couponService.updateById(coupon);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("coupon:coupon:delete")
    public R delete(@RequestBody Long[] ids){
		couponService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
